﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.ZhaoLuService;
using org.in2bits.MyXls;

namespace CommunitySystem.PageView
{
    public partial class ZhaoLu_DaoRuKaoShengControl : UserControl
    {
        private ZhaoLuService.ZhaoLuServiceClient Client1 = new ZhaoLuServiceClient();
        public ZhaoLu_DaoRuKaoShengControl()
        {
            InitializeComponent();
            for (int i = DateTime.Today.Year; i > DateTime.Today.Year - 10; i--)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Content = i.ToString();
                cbi.Tag = i;
                nian.Items.Add(cbi);
            }
            nian.SelectedIndex = 0;

            Client1.CanDaoRuAllCompleted += new EventHandler<CanDaoRuAllCompletedEventArgs>(Client1_CanDaoRuAllCompleted);
            Client1.GetZhaoLu_DaoRuKaoShengModelCompleted += new EventHandler<GetZhaoLu_DaoRuKaoShengModelCompletedEventArgs>(Client1_GetZhaoLu_DaoRuKaoShengModelCompleted);
            Client1.DaoRuZhaoLuKaoShengCompleted += new EventHandler<DaoRuZhaoLuKaoShengCompletedEventArgs>(Client1_DaoRuZhaoLuKaoShengCompleted);
            Client1.ShanChongFuCompleted += new EventHandler<ShanChongFuCompletedEventArgs>(Client1_ShanChongFuCompleted);
            Client1.QiQuanCompleted += new EventHandler<QiQuanCompletedEventArgs>(Client1_QiQuanCompleted);
            Load();
        }

        private void Load()
        {
            Client1.GetZhaoLu_DaoRuKaoShengModelAsync(DateTime.Today.Year - nian.SelectedIndex, false, yue.SelectedIndex + 1, 0);
        }

        void Client1_GetZhaoLu_DaoRuKaoShengModelCompleted(object sender, GetZhaoLu_DaoRuKaoShengModelCompletedEventArgs e)
        {
            DGRole.ItemsSource = e.Result;
        }

        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }

        private void QiQuan_Click(object sender, RoutedEventArgs e)
        {
           if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel i in (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
           if (selectedempmodel.Count > 0)
           {
               if(MessageBox.Show("确定进行弃权操作吗？","提示",MessageBoxButton.OKCancel) == MessageBoxResult.OK)
               {
                   string ss = "";
                   foreach (ZhaoLu_DaoRuKaoShengModel i in selectedempmodel)
                   {
                       ss += i.ID.ToString() + ",";
                   }
                   Client1.QiQuanAsync(ss.Substring(0, ss.Length - 1),"面试前");
               }
               
           }
        }
        void Client1_QiQuanCompleted(object sender, QiQuanCompletedEventArgs e)
        {
            MessageBox.Show("弃权成功！");
            Load();
        }

        private ObservableCollection<ZhaoLu_DaoRuKaoShengModel> selectedempmodel =
            new ObservableCollection<ZhaoLu_DaoRuKaoShengModel>();
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (ZhaoLu_DaoRuKaoShengModel p in DGRole.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                //CheckBox cb1 = DGRole.Columns[0].GetCellContent(p) as CheckBox;
                //cb1.IsChecked = p.IsSelected;
                if (p.IsSelected)
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(p);
                    }
                }
                else
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() != null)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault());
                    }
                }
            }

        }

        public ObservableCollection<ZhaoLu_DaoRuKaoShengModel> InEmpEducation =
            new ObservableCollection<ZhaoLu_DaoRuKaoShengModel>();
        private void btnIn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog dialog = new OpenFileDialog()
                                            {
                                                //Filter = "Excel files(2007~2010) (*.xlsx)|*.xlsx|Excel files(2003) (*.xls)|*.xls", //"Excel files (*.xlsx)|*.xlsx|(*.xls)|*.xls",
                                                Filter = "Excel files(2003) (*.xls)|*.xls",
                                                //"Excel files (*.xlsx)|*.xlsx|(*.xls)|*.xls",
                                                Multiselect = false

                                            };
                if ((bool)dialog.ShowDialog())
                {
                    Stream stream = dialog.File.OpenRead();
                    XlsDocument doc = new XlsDocument(stream);
                    Worksheet sheet1 = doc.Workbook.Worksheets[0];
                    #region 导入数据
                    for (ushort i = 2; i <= sheet1.Rows.Count - 1; i++)
                    {
                        if (sheet1.Rows[i].CellCount == 0)
                        {
                            break;
                        }
                        ZhaoLu_DaoRuKaoShengModel tem = new ZhaoLu_DaoRuKaoShengModel();
                        tem.AddDay = DateTime.Today;
                        tem.Year = DateTime.Today.Year - nian.SelectedIndex;
                        tem.Month = yue.SelectedIndex + 1;
                        tem.IsFangQi = false;
                        for (ushort ii = 1; ii <= sheet1.Rows[i].CellCount; ii++)
                        {
                            int column = sheet1.Rows[i].GetCell(ii).Column;
                            switch (column)
                            {
                                case 1:
                                    tem.ZCBH = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 2:
                                    tem.XingMing = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 3:
                                    tem.SFZH = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 4:
                                    tem.ZWID = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 5:
                                    tem.ZLDW = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 6:
                                    tem.YRBM = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 7:
                                    tem.BBZW = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 8:
                                    tem.ZKKM = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 9:
                                    tem.SL = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 10:
                                    tem.XC = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;

                                case 11:
                                    tem.ZF = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 12:
                                    tem.ZongFen = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 13:
                                    tem.XB = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 14:
                                    tem.CSRQ = sheet1.Rows[i].GetCell(ii).Value == null ? null : DateTime.FromOADate(Convert.ToDouble(sheet1.Rows[i].GetCell(ii).Value)).ToShortDateString();
                                    break;
                                case 15:
                                    tem.JKCD = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 16:
                                    tem.SG = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 17:
                                    tem.TZ = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 18:
                                    tem.MZ = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 19:
                                    tem.HYZK = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 20:
                                    tem.POXL = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;

                                case 21:
                                    tem.KSSF = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 22:
                                    tem.XL = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 23:
                                    tem.ZZMM = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 24:
                                    tem.XW = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 25:
                                    tem.BKBYYX = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 26:
                                    tem.BKZY = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 27:
                                    tem.BKBYSJ = sheet1.Rows[i].GetCell(ii).Value == null ? null : DateTime.FromOADate(Convert.ToDouble(sheet1.Rows[i].GetCell(ii).Value)).ToShortDateString();
                                    break;
                                case 28:
                                    tem.SSYX = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 29:
                                    tem.SSZY = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 30:
                                    tem.SSBYSJ = sheet1.Rows[i].GetCell(ii).Value == null ? null : DateTime.FromOADate(Convert.ToDouble(sheet1.Rows[i].GetCell(ii).Value)).ToShortDateString();
                                    break;
                                case 31:
                                    tem.BSYX = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;

                                case 32:
                                    tem.BSZY = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 33:
                                    tem.BSBYSJ = sheet1.Rows[i].GetCell(ii).Value == null ? null : DateTime.FromOADate(Convert.ToDouble(sheet1.Rows[i].GetCell(ii).Value)).ToShortDateString();
                                    break;
                                case 34:
                                    tem.GRJL = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 35:
                                    tem.TC = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 36:
                                    tem.SXZYKC = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 37:
                                    tem.WY1 = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 38:
                                    tem.WY2 = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 39:
                                    tem.WYDJ1 = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 40:
                                    tem.WYDJ2 = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 41:
                                    tem.JSJSP = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;

                                case 42:
                                    tem.TXDZ = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 43:
                                    tem.LXFS = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 44:
                                    tem.SJHM = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 45:
                                    tem.GZDW = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 46:
                                    tem.GZNX = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 47:
                                    tem.BYSJB = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 48:
                                    tem.SY = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 49:
                                    tem.HJ = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;
                                case 50:
                                    tem.YB = sheet1.Rows[i].GetCell(ii).Value == null ? null : sheet1.Rows[i].GetCell(ii).Value.ToString();
                                    break;

                            }
                        }
                        tem.IsFangQi = false;
                        InEmpEducation.Add(tem);
                    }
                    #endregion
                    if (InEmpEducation.Count > 0)
                    {
                        
                        Client1.CanDaoRuAllAsync(DateTime.Today.Year - nian.SelectedIndex,yue.SelectedIndex + 1);
                        
                    }
                }
            }
            catch (IOException ex)
            {
                //SilverlightMessageBoxLibrary.Message.ErrorMessage(ex.Message);
                SilverlightMessageBoxLibrary.Message.ErrorMessage("文件正在使用，请关闭文件再次尝试！");
            }
        }

        private int count = 0;
        void Client1_CanDaoRuAllCompleted(object sender, CanDaoRuAllCompletedEventArgs e)
        {
            if(e.Result == true)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel i in InEmpEducation)
                {
                    Client1.DaoRuZhaoLuKaoShengAsync(i);
                    count += 1;
                }
                
            }
            else
            {
                if(MessageBox.Show("本年度已导入过考生信息，是否覆盖？","提示",MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {

                    Client1.ShanChongFuAsync(Convert.ToInt32(InEmpEducation[0].Year),yue.SelectedIndex + 1);
                }
            }
        }
        void Client1_ShanChongFuCompleted(object sender, ShanChongFuCompletedEventArgs e)
        {
            foreach (ZhaoLu_DaoRuKaoShengModel i in InEmpEducation)
            {
                Client1.DaoRuZhaoLuKaoShengAsync(i);
                count += 1;
            }
        }
        void Client1_DaoRuZhaoLuKaoShengCompleted(object sender, DaoRuZhaoLuKaoShengCompletedEventArgs e)
        {
            if (count == InEmpEducation.Count)
            {
                count = count - 1;
                SilverlightMessageBoxLibrary.Message.InfoMessage("导入成功！");
                Load();
            }
            
        }
    }
}
